/*
 * Copyright 2010 bedatadriven
 *
 *    Licensed under the Apache License, Version 2.0 (the "License");
 *    you may not use this file except in compliance with the License.
 *    You may obtain a copy of the License at
 *
 *        http://www.apache.org/licenses/LICENSE-2.0
 *
 *    Unless required by applicable law or agreed to in writing, software
 *    distributed under the License is distributed on an "AS IS" BASIS,
 *    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *    See the License for the specific language governing permissions and
 *    limitations under the License.
 */

package com.bedatadriven.mso.excel;

public class NumberFormat {
  private String formatString;

  public NumberFormat(String formatString) {
    this.formatString = formatString;
  }

  public boolean isMixed() {
    return formatString == null;
  }

  public boolean isGeneral() {
    return "General".equals(formatString);
  }

  public boolean isPercent() {
    return formatString != null && formatString.endsWith("%");
  }

  public String getFormatString() {
    return formatString;
  }

  public static NumberFormat percent() {
    return new NumberFormat("0%");
  }

  public static NumberFormat decimal(int numDecimals) {
    // enable static optimization by GWT compiler
    // for common cases
    if(numDecimals == 0) {
      return new NumberFormat("0");
    } else if(numDecimals == 1) {
      return new NumberFormat("0.0");
    } else {
      StringBuilder sb = new StringBuilder("0.");
      for(int i=0;i!=numDecimals;++i) {
        sb.append("0");
      }
      return new NumberFormat(sb.toString());
    }
  }
}
